package com.springboot.frame.backup;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

/**
 * @author liheng
 * @ClassName DBConnUtils
 * @Description
 * @date 2021-03-02 17:45
 */
@Slf4j
public class DBConnUtils {
    //建立连接
    public static Connection getConnection(BackUpProperties backUpProperties) {
        Connection conn = null;
        try {
            // Step 2: Opening database connection
            // Step 2.A: Create and get connection using DriverManager class
            Class.forName(backUpProperties.getDbType().getDriver());
            //加载ucanaccess驱动
            String DB_URL = "jdbc:" + backUpProperties.getDbType().getDb() + "://" + backUpProperties.getHost() + ":" + backUpProperties.getPort() + "/" + backUpProperties.getExportDatabaseName() + "?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
            conn = DriverManager.getConnection(DB_URL, backUpProperties.getUsername(), backUpProperties.getPassword());
            log.debug(backUpProperties.getDbType().getDb() + " connection success");
        } catch (Exception e) {
            log.error(backUpProperties.getDbType().getDb() + " connection fail");
        }
        return conn;
    }
}
